using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using Spacejock;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using yW5Lib;

namespace yWriter6
{
	public class clsPrintChapter
	{
		public delegate void PrintProgressEventHandler(string ProgressString);

		[CompilerGenerated]
		private PrintProgressEventHandler PrintProgressEvent;

		private clsPrintScene cPC;

		private Collection mvarIDs;

		public event PrintProgressEventHandler PrintProgress
		{
			[CompilerGenerated]
			add
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Combine(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Remove(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
		}

		public clsPrintChapter()
		{
			cPC = new clsPrintScene();
			mvarIDs = new Collection();
		}

		public void ClearIDs()
		{
			mvarIDs.Clear();
		}

		public void AddID(int ID)
		{
			mvarIDs.Add(ID);
		}

		public void PrintChapters(clsProject cProject, bool SceneTextOnly, string TagFilter, int VPID)
		{
			clsReport Report = new clsReport(ref clsShared.cLog, ref clsShared.cSettings, cProject.Title + " by " + cProject.AuthorName);
			clsShared.cSubs.BusyMouse();
			Report.AddTitle();
			if (Operators.CompareString(TagFilter.Trim(), "", TextCompare: false) != 0)
			{
				Report.AddCentered("Only scenes with the following tag: " + TagFilter);
			}
			Report.AddBR();
			int num = 0;
			IEnumerator enumerator = default(IEnumerator);
			try
			{
				enumerator = mvarIDs.GetEnumerator();
				while (enumerator.MoveNext())
				{
					int chID = Conversions.ToInteger(enumerator.Current);
					LocalPrintChapter(ref Report, cProject, chID, SceneTextOnly, TagFilter, VPID);
					num++;
				}
			}
			finally
			{
				if (enumerator is IDisposable)
				{
					(enumerator as IDisposable).Dispose();
				}
			}
			clsShared.cSubs.NormalMouse();
			Report.AddFooter(App.Title);
			Report.Show();
			PrintProgressEvent?.Invoke("Printing Complete");
			ClearIDs();
		}

		public void PrintChapter(int ChID, clsProject cProject, bool SceneTextOnly, string TagFilter, int VPID)
		{
			ClearIDs();
			mvarIDs.Add(ChID);
			PrintChapters(cProject, SceneTextOnly, TagFilter, VPID);
		}

		private void LocalPrintChapter(ref clsReport Report, clsProject cProject, int ChID, bool SceneTextOnly, string TagFilter, int VPID)
		{
			try
			{
				clsChapter clsChapter = cProject.get_ChapterByID(ChID);
				PrintProgressEvent?.Invoke("Printing " + clsChapter.Title);
				Report.AddCentered("\r\n\r\n<h1>" + clsChapter.Title + "</h1>\r\n");
				if (clsChapter.Unused)
				{
					Report.AddCentered("<h2>(Unused Chapter)</h2>\r\n");
				}
				Report.OpenTable();
				Report.OpenRow();
				Report.OpenCell();
				int sceneCount = clsChapter.SceneCount;
				bool flag = default(bool);
				for (int i = 1; i <= sceneCount; i++)
				{
					int num = clsChapter.get_SceneIDByIndex(i);
					clsScene clsScene = cProject.get_SceneByID(num);
					if (clsScene.PassesTagFilter(TagFilter) && ((VPID <= 0) | (clsScene.ViewPointID == VPID)))
					{
						if (flag)
						{
							if (SceneTextOnly)
							{
								if (!clsScene.AppendToPrevious)
								{
									Report.AddCentered("\r\n* * *\r\n\r\n");
								}
							}
							else
							{
								Report.PageBreak();
							}
						}
						string left = clsScene.ConvertedToPlainText().Trim();
						if (Operators.CompareString(left, "", TextCompare: false) == 0 && SceneTextOnly)
						{
							Report.AddCentered("No scene content");
						}
						else
						{
							cPC.PrintScene(ref Report, cProject, num, SceneTextOnly);
							if (Operators.CompareString(left, "", TextCompare: false) == 0)
							{
								Report.AddCentered("No scene content");
							}
						}
						flag = true;
					}
				}
				Report.CloseCell();
				Report.CloseRow();
				Report.CloseTable();
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}
	}
}
